package com.gdupt.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdupt.entity.CustomerInfo;
import com.gdupt.entity.DrawPlan;
import com.gdupt.entity.SalesOpportunity;
import com.gdupt.entity.vo.ResponseData;
import com.gdupt.service.ICustomerInfoService;
import com.gdupt.service.IDrawPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 制定计划 制定计划 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
@RestController
@RequestMapping("/draw-plan")
public class DrawPlanController {

    @Autowired
    private IDrawPlanService iDrawPlanService;

    @Autowired
    private ICustomerInfoService iCustomerInfoService;
    @RequestMapping("/list/{id}")
    public ResponseData drawPlan(Integer page,Integer limit,@PathVariable("id") Integer id) {
        Page<DrawPlan> page1 = new Page<>(page,limit);
        List<DrawPlan> drawPlans =iDrawPlanService.findAllPlanById(page1,id);
        return  ResponseData.okPage(page1.getTotal(),drawPlans);
    }
    @RequestMapping("/savePlan")
    public  ResponseData savePlan(Page page,DrawPlan drawPlan){
        Integer sta=0;
        List<DrawPlan> drawPlans =iDrawPlanService.findAllPlanById(page,drawPlan.getOpportunityId());
        for (DrawPlan plan : drawPlans) {
            sta=plan.getStatus();
            System.out.println(sta);
            if(drawPlan.getPlanItem().equals(plan.getPlanItem())&&drawPlan.getCreationTime().equals(plan.getCreationTime())){
                return ResponseData.failMsg("添加计划失败！！，计划内容和时间与之前的添加的一致！");
            }
        }
        if(sta==0){
            iDrawPlanService.savePlan(drawPlan);
            return ResponseData.failMsg("添加计划成功！！");
        }
        return ResponseData.failMsg("添加计划失败！客户已开发成功或终止了开发！！");
    }

    @PostMapping("/editPlan")
    public  ResponseData editPlan(DrawPlan drawPlan){
        boolean flg =iDrawPlanService.editPlan(drawPlan);
        if(flg){
            return ResponseData.okMsg("修改计划成功！！");
        }
        return ResponseData.failMsg("修改计划失败！客户已开发成功！");
    }

    @PostMapping("/editResult")
    public  ResponseData editResult(DrawPlan drawPlan){
        System.out.println(drawPlan);
        boolean flg =iDrawPlanService.editPlanResult(drawPlan);
        if(flg){
            return ResponseData.okMsg("修改成功！！");
        }
        return ResponseData.failMsg("修改失败！客户已开发成功！");
    }

    @PostMapping("/delete/{id}")
    public  ResponseData deletePlan(@PathVariable("id") Integer id){
        boolean flg = iDrawPlanService.deletePlan(id);
        System.out.println(id);
        if(flg){
            return ResponseData.okMsg("删除成功！！");
        }
         return ResponseData.failMsg("删除失败！客户已经开发成功！");
    }

    @PostMapping("/deleteBySId/{id}")
    public  ResponseData deletePlanBySId(@PathVariable("id") Integer id){
        boolean flg =iDrawPlanService.failPLan(id);
        if(flg){
            return ResponseData.okMsg("终止开发成功！");
        }
        return ResponseData.failMsg("终止开发失败！");
    }

    @PostMapping("/saveCustomerInfo")
    public   ResponseData saveCustomerInfo(CustomerInfo customerInfo){
        boolean flg = false;
        Integer id=customerInfo.getCustomerId().intValue();
        try {
            flg = iCustomerInfoService.save(customerInfo);
             iDrawPlanService.sucessPLan(id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(flg){
                return ResponseData.okMsg("添加客户成功！计划项已不可修改！");
            }
            return ResponseData.failMsg("添加客户失败！客户已存在！");
        }
    }

}

